package com.fullertontech.framework.generator;

import java.util.Properties;
import java.util.Set;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.internal.util.StringUtility;

public class MyCommentGenerator implements CommentGenerator{

	    //开始的分隔符，例如mysql为`，sqlserver为[
	    private String beginningDelimiter = "";
	    //结束的分隔符，例如mysql为`，sqlserver为]
	    private String endingDelimiter = "";


	    public void addJavaFileComment(CompilationUnit compilationUnit) {
	        return;
	    }

	    /**
	     * xml中的注释
	     *
	     * @param xmlElement
	     */
	    public void addComment(XmlElement xmlElement) {
	        xmlElement.addElement(new TextElement("<!--"));
	        StringBuilder sb = new StringBuilder();
	        sb.append("  WARNING - ");
	        sb.append(MergeConstants.NEW_ELEMENT_TAG);
	        xmlElement.addElement(new TextElement(sb.toString()));
	        xmlElement.addElement(new TextElement("-->"));
	    }

	    public void addRootComment(XmlElement rootElement) {
	        return;
	    }

	    public void addConfigurationProperties(Properties properties) {
	        String beginningDelimiter = properties.getProperty("beginningDelimiter");
	        if (StringUtility.stringHasValue(beginningDelimiter)) {
	            this.beginningDelimiter = beginningDelimiter;
	        }
	        String endingDelimiter = properties.getProperty("endingDelimiter");
	        if (StringUtility.stringHasValue(endingDelimiter)) {
	            this.endingDelimiter = endingDelimiter;
	        }
	    }

	    public String getDelimiterName(String name) {
	        StringBuilder nameBuilder = new StringBuilder();
	        nameBuilder.append(beginningDelimiter);
	        nameBuilder.append(name);
	        nameBuilder.append(endingDelimiter);
	        return nameBuilder.toString();
	    }

	    /**
	     * 删除标记
	     *
	     * @param javaElement
	     * @param markAsDoNotDelete
	     */
	    protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
	        StringBuilder sb = new StringBuilder();
	        sb.append(" * ");
	        sb.append(MergeConstants.NEW_ELEMENT_TAG);
	        if (markAsDoNotDelete) {
	            sb.append(" do_not_delete_during_merge");
	        }
	        javaElement.addJavaDocLine(sb.toString());
	    }

	    /**
	     * Example使用
	     *
	     * @param innerClass
	     * @param introspectedTable
	     */
	    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
	    	System.out.println(introspectedTable.getTableConfiguration().getTableName());
	    	innerClass.addAnnotation("@Table(\""+introspectedTable.getTableConfiguration().getTableName()+"\")");
	    }

	    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
	    }

	    /**
	     * 给字段添加数据库备注
	     *
	     * @param field
	     * @param introspectedTable
	     * @param introspectedColumn
	     */
	    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
	        if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
	            field.addJavaDocLine("/**");
	            StringBuilder sb = new StringBuilder();
	            sb.append(" * ");
	            sb.append(introspectedColumn.getRemarks());
	            field.addJavaDocLine(sb.toString());
	            field.addJavaDocLine(" */");
	        }
	        //添加注解
	        if (field.isTransient()) {
	            //@Column
	            field.addAnnotation("@Transient");
	        }
	        for (IntrospectedColumn column : introspectedTable.getPrimaryKeyColumns()) {
	            if (introspectedColumn == column) {
	                field.addAnnotation("@Id");
	                break;
	            }
	        }
	        String column = introspectedColumn.getActualColumnName();
	        if (StringUtility.stringContainsSpace(column) || introspectedTable.getTableConfiguration().isAllColumnDelimitingEnabled()) {
	            column = introspectedColumn.getContext().getBeginningDelimiter()
	                    + column
	                    + introspectedColumn.getContext().getEndingDelimiter();
	        }
	        if (!column.equals(introspectedColumn.getJavaProperty())) {
	            //@Column
	            field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
	        } else if (StringUtility.stringHasValue(beginningDelimiter) || StringUtility.stringHasValue(endingDelimiter)) {
	            field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
	        }
	        if (introspectedColumn.isIdentity()) {
	            if (introspectedTable.getTableConfiguration().getGeneratedKey().getRuntimeSqlStatement().equals("JDBC")) {
	                field.addAnnotation("@GeneratedValue(generator = \"JDBC\")");
	            } else {
	                field.addAnnotation("@GeneratedValue(strategy = GenerationType.IDENTITY)");
	            }
	        } else if (introspectedColumn.isSequenceColumn()) {
	            field.addAnnotation("@SequenceGenerator(name=\"\",sequenceName=\"" + introspectedTable.getTableConfiguration().getGeneratedKey().getRuntimeSqlStatement() + "\")");
	        }
	    }

	    /**
	     * Example使用
	     *
	     * @param field
	     * @param introspectedTable
	     */
	    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
	    }

	    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
	    	FullyQualifiedJavaType tableType=new FullyQualifiedJavaType("javax.persistence.Table");
	    	FullyQualifiedJavaType columnType=new FullyQualifiedJavaType("javax.persistence.Column");
	    	FullyQualifiedJavaType idType=new FullyQualifiedJavaType("javax.persistence.Id");
	    	topLevelClass.addAnnotation("@Table(name=\""+introspectedTable.getTableConfiguration().getTableName()+"\")");
	    	topLevelClass.addImportedType(tableType);
	    	topLevelClass.addImportedType(columnType);
	    	topLevelClass.addImportedType(idType);
	    }

	    /**
	     * @param method
	     * @param introspectedTable
	     */
	    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
	    }

	    /**
	     * getter方法注释
	     *
	     * @param method
	     * @param introspectedTable
	     * @param introspectedColumn
	     */
	    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
	        StringBuilder sb = new StringBuilder();
	        method.addJavaDocLine("/**");
	        if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
	            sb.append(" * 获取");
	            sb.append(introspectedColumn.getRemarks());
	            method.addJavaDocLine(sb.toString());
	            method.addJavaDocLine(" *");
	        }
	        sb.setLength(0);
	        sb.append(" * @return ");
	        sb.append(introspectedColumn.getActualColumnName());
	        if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
	            sb.append(" - ");
	            sb.append(introspectedColumn.getRemarks());
	        }
	        method.addJavaDocLine(sb.toString());
	        method.addJavaDocLine(" */");
	    }

	    /**
	     * setter方法注释
	     *
	     * @param method
	     * @param introspectedTable
	     * @param introspectedColumn
	     */
	    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
	        StringBuilder sb = new StringBuilder();
	        method.addJavaDocLine("/**");
	        if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
	            sb.append(" * 设置");
	            sb.append(introspectedColumn.getRemarks());
	            method.addJavaDocLine(sb.toString());
	            method.addJavaDocLine(" *");
	        }
	        Parameter parm = method.getParameters().get(0);
	        sb.setLength(0);
	        sb.append(" * @param ");
	        sb.append(parm.getName());
	        if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
	            sb.append(" ");
	            sb.append(introspectedColumn.getRemarks());
	        }
	        method.addJavaDocLine(sb.toString());
	        method.addJavaDocLine(" */");
	    }

	    /**
	     * Example使用
	     *
	     * @param innerClass
	     * @param introspectedTable
	     * @param markAsDoNotDelete
	     */
	    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
	    }

		@Override
		public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
				Set<FullyQualifiedJavaType> imports) {
		}

		@Override
		public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
				IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
		}

		@Override
		public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
				Set<FullyQualifiedJavaType> imports) {
		}

		@Override
		public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
				IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
		}

		@Override
		public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
				Set<FullyQualifiedJavaType> imports) {
		}

}
